﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_TempEdu : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IMasEducationQualificationBLL qualBll = new MasEducationQualificationBLL();
        private IMasEducationInstituteBLL insBLL = new MasEducationInstituteBLL();
        private IMasEducationMajorBLL mjBLL = new MasEducationMajorBLL();
        private RecruitApplicantBLL appBLL = new RecruitApplicantBLL();

        private object _dataItem = null;
        private const int ItemsPerRequest = 10;

        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }
            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    cmbQulification.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_QUAL_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_QUAL_SEQ").ToString()));
                    cmbMajor.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_MAJOR_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_MAJOR_SEQ").ToString()));
                    cmbInstitues.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_INS_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_INS_SEQ").ToString()));
                    
                    cmbQulification.SelectedValue = DataBinder.Eval(DataItem, "EDU_QUAL_SEQ").ToString();
                    cmbMajor.SelectedValue = DataBinder.Eval(DataItem, "EDU_MAJOR_SEQ").ToString();
                    cmbInstitues.SelectedValue = DataBinder.Eval(DataItem, "EDU_INS_SEQ").ToString();
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            appBLL.InsertEducations(Int32.Parse(Session["tmp_emp_seq"].ToString())
                , Int32.Parse(cmbQulification.SelectedValue)
                , Int32.Parse(cmbMajor.SelectedValue)
                , Int32.Parse(cmbInstitues.SelectedValue)
                , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            appBLL.UpdateEducations(Int32.Parse(lblTmpEduSeq.Text)
                , Int32.Parse(cmbQulification.SelectedValue)
                , Int32.Parse(cmbMajor.SelectedValue)
                , Int32.Parse(cmbInstitues.SelectedValue)
                , ssMng.EmpSeqI);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbMajor_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbMajor.Items.Clear();

            DataTable dt = mjBLL.LoadEducationMajors((int?)null, e.Text).Tables[0];
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            //int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbMajor.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_MAJOR_NAME"].ToString(), dt.Rows[i]["EDU_MAJOR_SEQ"].ToString()));
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbInstitues_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbInstitues.Items.Clear();

            bool bLocal = rdoInternal.SelectedValue == "1";

            DataTable dt = insBLL.LoadEducationInstitutes((int?)null, bLocal, e.Text).Tables[0];
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbInstitues.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_INS_NAME"].ToString(), dt.Rows[i]["EDU_INS_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdFieldName = new TableCell();
                Label lblFieldName = new Label();
                lblFieldName.Text = dt.Rows[i]["EDU_INS_NAME"].ToString();
                tdFieldName.Width = Unit.Pixel(200);
                tdFieldName.Controls.Add(lblFieldName);
                tr.Cells.Add(tdFieldName);

                TableCell tdPosTypeName = new TableCell();
                Label lblPosTypeName = new Label();
                lblPosTypeName.Text = dt.Rows[i]["COUNTRY_NAME"].ToString();
                tdPosTypeName.Width = Unit.Pixel(100);
                tdPosTypeName.Controls.Add(lblPosTypeName);
                tr.Cells.Add(tdPosTypeName);

                ((Table)cmbInstitues.Items[j].FindControl("tableInstitues")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbQulification_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbQulification.Items.Clear();

            DataTable dt = qualBll.LoadEducationQualifications((int?)null, (int?)null, e.Text).Tables[0];
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbQulification.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_QUAL_NAME"].ToString(), dt.Rows[i]["EDU_QUAL_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdFieldName = new TableCell();
                Label lblFieldName = new Label();
                lblFieldName.Text = dt.Rows[i]["EDU_QUAL_NAME"].ToString();
                tdFieldName.Width = Unit.Pixel(200);
                tdFieldName.Controls.Add(lblFieldName);
                tr.Cells.Add(tdFieldName);

                TableCell tdPosTypeName = new TableCell();
                Label lblPosTypeName = new Label();
                lblPosTypeName.Text = dt.Rows[i]["EDU_DEGREE_NAME"].ToString();
                tdPosTypeName.Width = Unit.Pixel(100);
                tdPosTypeName.Controls.Add(lblPosTypeName);
                tr.Cells.Add(tdPosTypeName);

                ((Table)cmbQulification.Items[j].FindControl("tableQulification")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbInstitues_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }
    }
}